package com.digital.herbarium.service;

import com.digital.herbarium.Entity.Menu;
import com.digital.herbarium.Mapper.MenuMapper;
import com.digital.herbarium.Util.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuService {

    @Autowired
    MenuMapper menuMapper;


    public List<Menu> getMenuCategory(Menu menu){
        return  menuMapper.getMenuInfo(menu);
    }

    public List<Map<String,Object>> getMenuInfo(){
        List<Menu> menuList = menuMapper.getMenuInfo(new Menu());
        List<Map<String, Object>> treeList = new ArrayList<>();

        // 以 parentId 为0的元素为根节点，递归处理子节点
        for (Menu m : menuList) {
            if (m.getParentId() == 0) {
                Map<String, Object>  node = TreeUtil.createNode(m, menuList);
                treeList.add(node);
            }
        }

        return treeList;

    }

}


